package b.b.g.i;

import android.content.Context;
import android.content.res.Resources;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.SystemClock;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnAttachStateChangeListener;
import android.view.View.OnKeyListener;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.HeaderViewListAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow.OnDismissListener;
import b.b.h.t;
import b.b.h.u;
import b.i.k.o;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.WeakHashMap;

/* compiled from: CascadingMenuPopup */
public final class d extends k implements m, OnKeyListener, OnDismissListener {
    public boolean A;
    public final Context b;
    public final int c;
    public final int d;
    public final int e;
    public final boolean f;
    public final Handler g;
    public final List<g> h = new ArrayList();
    public final List<d> i = new ArrayList();
    public final OnGlobalLayoutListener j = new a();
    public final OnAttachStateChangeListener k = new b();
    public final t l = new c();
    public int m;
    public int n;
    public View o;
    public View p;
    public int q;
    public boolean r;
    public boolean s;
    public int t;
    public int u;
    public boolean v;
    public boolean w;
    public b.b.g.i.m.a x;
    public ViewTreeObserver y;
    public OnDismissListener z;

    /* compiled from: CascadingMenuPopup */
    public class a implements OnGlobalLayoutListener {
        public void onGlobalLayout() {
            if (d.this.b() && d.this.i.size() > 0 && !((d) d.this.i.get(0)).a.y) {
                View view = d.this.p;
                if (view == null || !view.isShown()) {
                    d.this.dismiss();
                    return;
                }
                for (d dVar : d.this.i) {
                    dVar.a.d();
                }
            }
        }
    }

    /* compiled from: CascadingMenuPopup */
    public class b implements OnAttachStateChangeListener {
        public void onViewAttachedToWindow(View view) {
        }

        public void onViewDetachedFromWindow(View view) {
            ViewTreeObserver viewTreeObserver = d.this.y;
            if (viewTreeObserver != null) {
                if (!viewTreeObserver.isAlive()) {
                    d.this.y = view.getViewTreeObserver();
                }
                d dVar = d.this;
                dVar.y.removeGlobalOnLayoutListener(dVar.j);
            }
            view.removeOnAttachStateChangeListener(this);
        }
    }

    /* compiled from: CascadingMenuPopup */
    public static class d {
        public final u a;
        public final g b;
        public final int c;

        public d(u uVar, g gVar, int i) {
            this.a = uVar;
            this.b = gVar;
            this.c = i;
        }
    }

    /* compiled from: CascadingMenuPopup */
    public class c implements t {

        /* compiled from: CascadingMenuPopup */
        public class a implements Runnable {
            public final /* synthetic */ d a;
            public final /* synthetic */ MenuItem b;
            public final /* synthetic */ g c;

            public a(d dVar, MenuItem menuItem, g gVar) {
                this.a = dVar;
                this.b = menuItem;
                this.c = gVar;
            }

            public void run() {
                d dVar = this.a;
                if (dVar != null) {
                    d.this.A = true;
                    dVar.b.c(false);
                    d.this.A = false;
                }
                if (this.b.isEnabled() && this.b.hasSubMenu()) {
                    this.c.r(this.b, 4);
                }
            }
        }

        public void c(g gVar, MenuItem menuItem) {
            d dVar = null;
            d.this.g.removeCallbacksAndMessages(null);
            int size = d.this.i.size();
            int i = 0;
            while (i < size) {
                if (gVar == ((d) d.this.i.get(i)).b) {
                    break;
                }
                i++;
            }
            i = -1;
            if (i != -1) {
                i++;
                if (i < d.this.i.size()) {
                    dVar = (d) d.this.i.get(i);
                }
                d.this.g.postAtTime(new a(dVar, menuItem, gVar), gVar, SystemClock.uptimeMillis() + 200);
            }
        }

        public void e(g gVar, MenuItem menuItem) {
            d.this.g.removeCallbacksAndMessages(gVar);
        }
    }

    public d(Context context, View view, int i, int i2, boolean z) {
        int i3 = 0;
        this.m = 0;
        this.n = 0;
        this.b = context;
        this.o = view;
        this.d = i;
        this.e = i2;
        this.f = z;
        this.v = false;
        WeakHashMap weakHashMap = o.a;
        if (view.getLayoutDirection() != 1) {
            i3 = 1;
        }
        this.q = i3;
        Resources resources = context.getResources();
        this.c = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(2131165207));
        this.g = new Handler();
    }

    public void a(g gVar, boolean z) {
        int size = this.i.size();
        int i = 0;
        while (i < size) {
            if (gVar == ((d) this.i.get(i)).b) {
                break;
            }
            i++;
        }
        i = -1;
        if (i >= 0) {
            size = i + 1;
            if (size < this.i.size()) {
                ((d) this.i.get(size)).b.c(false);
            }
            d dVar = (d) this.i.remove(i);
            dVar.b.u(this);
            if (this.A) {
                u uVar = dVar.a;
                Objects.requireNonNull(uVar);
                if (VERSION.SDK_INT >= 23) {
                    uVar.z.setExitTransition(null);
                }
                dVar.a.z.setAnimationStyle(0);
            }
            dVar.a.dismiss();
            size = this.i.size();
            if (size > 0) {
                this.q = ((d) this.i.get(size - 1)).c;
            } else {
                View view = this.o;
                WeakHashMap weakHashMap = o.a;
                this.q = view.getLayoutDirection() == 1 ? 0 : 1;
            }
            if (size == 0) {
                dismiss();
                b.b.g.i.m.a aVar = this.x;
                if (aVar != null) {
                    aVar.a(gVar, true);
                }
                ViewTreeObserver viewTreeObserver = this.y;
                if (viewTreeObserver != null) {
                    if (viewTreeObserver.isAlive()) {
                        this.y.removeGlobalOnLayoutListener(this.j);
                    }
                    this.y = null;
                }
                this.p.removeOnAttachStateChangeListener(this.k);
                this.z.onDismiss();
            } else if (z) {
                ((d) this.i.get(0)).b.c(false);
            }
        }
    }

    public boolean b() {
        return this.i.size() > 0 && ((d) this.i.get(0)).a.b();
    }

    public void d() {
        if (!b()) {
            for (g w : this.h) {
                w(w);
            }
            this.h.clear();
            View view = this.o;
            this.p = view;
            if (view != null) {
                Object obj = this.y == null ? 1 : null;
                ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
                this.y = viewTreeObserver;
                if (obj != null) {
                    viewTreeObserver.addOnGlobalLayoutListener(this.j);
                }
                this.p.addOnAttachStateChangeListener(this.k);
            }
        }
    }

    public void dismiss() {
        int size = this.i.size();
        if (size > 0) {
            d[] dVarArr = (d[]) this.i.toArray(new d[size]);
            for (size--; size >= 0; size--) {
                d dVar = dVarArr[size];
                if (dVar.a.b()) {
                    dVar.a.dismiss();
                }
            }
        }
    }

    public boolean e(r rVar) {
        for (d dVar : this.i) {
            if (rVar == dVar.b) {
                dVar.a.c.requestFocus();
                return true;
            }
        }
        if (!rVar.hasVisibleItems()) {
            return false;
        }
        rVar.b(this, this.b);
        if (b()) {
            w(rVar);
        } else {
            this.h.add(rVar);
        }
        b.b.g.i.m.a aVar = this.x;
        if (aVar != null) {
            aVar.b(rVar);
        }
        return true;
    }

    public void f(boolean z) {
        for (d dVar : this.i) {
            ListAdapter adapter = dVar.a.c.getAdapter();
            if (adapter instanceof HeaderViewListAdapter) {
                adapter = ((HeaderViewListAdapter) adapter).getWrappedAdapter();
            }
            ((f) adapter).notifyDataSetChanged();
        }
    }

    public ListView g() {
        if (this.i.isEmpty()) {
            return null;
        }
        List list = this.i;
        return ((d) list.get(list.size() - 1)).a.c;
    }

    public boolean h() {
        return false;
    }

    public void k(b.b.g.i.m.a aVar) {
        this.x = aVar;
    }

    public void l(g gVar) {
        gVar.b(this, this.b);
        if (b()) {
            w(gVar);
        } else {
            this.h.add(gVar);
        }
    }

    public boolean m() {
        return false;
    }

    public void o(View view) {
        if (this.o != view) {
            this.o = view;
            int i = this.m;
            WeakHashMap weakHashMap = o.a;
            this.n = Gravity.getAbsoluteGravity(i, view.getLayoutDirection());
        }
    }

    public void onDismiss() {
        d dVar;
        int size = this.i.size();
        for (int i = 0; i < size; i++) {
            dVar = (d) this.i.get(i);
            if (!dVar.a.b()) {
                break;
            }
        }
        dVar = null;
        if (dVar != null) {
            dVar.b.c(false);
        }
    }

    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() != 1 || i != 82) {
            return false;
        }
        dismiss();
        return true;
    }

    public void p(boolean z) {
        this.v = z;
    }

    public void q(int i) {
        if (this.m != i) {
            this.m = i;
            View view = this.o;
            WeakHashMap weakHashMap = o.a;
            this.n = Gravity.getAbsoluteGravity(i, view.getLayoutDirection());
        }
    }

    public void r(int i) {
        this.r = true;
        this.t = i;
    }

    public void s(OnDismissListener onDismissListener) {
        this.z = onDismissListener;
    }

    public void t(boolean z) {
        this.w = z;
    }

    public void u(int i) {
        this.s = true;
        this.u = i;
    }

    /* JADX WARNING: Removed duplicated region for block: B:79:0x01b9  */
    /* JADX WARNING: Removed duplicated region for block: B:39:0x00ea  */
    /* JADX WARNING: Missing block: B:54:0x0147, code skipped:
            if (((r9.getWidth() + r11[0]) + r5) > r10.right) goto L_0x0152;
     */
    /* JADX WARNING: Missing block: B:56:0x014e, code skipped:
            if ((r11[0] - r5) < 0) goto L_0x0150;
     */
    /* JADX WARNING: Missing block: B:58:0x0152, code skipped:
            r9 = 0;
     */
    public final void w(b.b.g.i.g r17) {
        /*
        r16 = this;
        r0 = r16;
        r1 = r17;
        r2 = android.os.Build.VERSION.SDK_INT;
        r3 = r0.b;
        r3 = android.view.LayoutInflater.from(r3);
        r4 = new b.b.g.i.f;
        r5 = r0.f;
        r6 = 2131492875; // 0x7f0c000b float:1.8609214E38 double:1.053097404E-314;
        r4.m313init(r1, r3, r5, r6);
        r5 = r16.b();
        r6 = 1;
        if (r5 != 0) goto L_0x0024;
    L_0x001d:
        r5 = r0.v;
        if (r5 == 0) goto L_0x0024;
    L_0x0021:
        r4.c = r6;
        goto L_0x0030;
    L_0x0024:
        r5 = r16.b();
        if (r5 == 0) goto L_0x0030;
    L_0x002a:
        r5 = b.b.g.i.k.v(r17);
        r4.c = r5;
    L_0x0030:
        r5 = r0.b;
        r7 = r0.c;
        r8 = 0;
        r5 = b.b.g.i.k.n(r4, r8, r5, r7);
        r7 = new b.b.h.u;
        r9 = r0.b;
        r10 = r0.d;
        r11 = r0.e;
        r7.m7844init(r9, r8, r10, r11);
        r9 = r0.l;
        r7.D = r9;
        r7.q = r0;
        r9 = r7.z;
        r9.setOnDismissListener(r0);
        r9 = r0.o;
        r7.p = r9;
        r9 = r0.n;
        r7.l = r9;
        r7.s(r6);
        r9 = r7.z;
        r10 = 2;
        r9.setInputMethodMode(r10);
        r7.p(r4);
        r7.r(r5);
        r4 = r0.n;
        r7.l = r4;
        r4 = r0.i;
        r4 = r4.size();
        if (r4 <= 0) goto L_0x00e6;
    L_0x0072:
        r4 = r0.i;
        r11 = r4.size();
        r11 = r11 - r6;
        r4 = r4.get(r11);
        r4 = (b.b.g.i.d.d) r4;
        r11 = r4.b;
        r12 = r11.size();
        r13 = 0;
    L_0x0086:
        if (r13 >= r12) goto L_0x009c;
    L_0x0088:
        r14 = r11.getItem(r13);
        r15 = r14.hasSubMenu();
        if (r15 == 0) goto L_0x0099;
    L_0x0092:
        r15 = r14.getSubMenu();
        if (r1 != r15) goto L_0x0099;
    L_0x0098:
        goto L_0x009d;
    L_0x0099:
        r13 = r13 + 1;
        goto L_0x0086;
    L_0x009c:
        r14 = r8;
    L_0x009d:
        if (r14 != 0) goto L_0x00a0;
    L_0x009f:
        goto L_0x00e8;
    L_0x00a0:
        r11 = r4.a;
        r11 = r11.c;
        r12 = r11.getAdapter();
        r13 = r12 instanceof android.widget.HeaderViewListAdapter;
        if (r13 == 0) goto L_0x00b9;
    L_0x00ac:
        r12 = (android.widget.HeaderViewListAdapter) r12;
        r13 = r12.getHeadersCount();
        r12 = r12.getWrappedAdapter();
        r12 = (b.b.g.i.f) r12;
        goto L_0x00bc;
    L_0x00b9:
        r12 = (b.b.g.i.f) r12;
        r13 = 0;
    L_0x00bc:
        r15 = r12.getCount();
        r10 = 0;
    L_0x00c1:
        r8 = -1;
        if (r10 >= r15) goto L_0x00ce;
    L_0x00c4:
        r9 = r12.getItem(r10);
        if (r14 != r9) goto L_0x00cb;
    L_0x00ca:
        goto L_0x00cf;
    L_0x00cb:
        r10 = r10 + 1;
        goto L_0x00c1;
    L_0x00ce:
        r10 = -1;
    L_0x00cf:
        if (r10 != r8) goto L_0x00d2;
    L_0x00d1:
        goto L_0x00e7;
    L_0x00d2:
        r10 = r10 + r13;
        r8 = r11.getFirstVisiblePosition();
        r10 = r10 - r8;
        if (r10 < 0) goto L_0x00e7;
    L_0x00da:
        r8 = r11.getChildCount();
        if (r10 < r8) goto L_0x00e1;
    L_0x00e0:
        goto L_0x00e7;
    L_0x00e1:
        r8 = r11.getChildAt(r10);
        goto L_0x00e8;
    L_0x00e6:
        r4 = 0;
    L_0x00e7:
        r8 = 0;
    L_0x00e8:
        if (r8 == 0) goto L_0x01b9;
    L_0x00ea:
        r9 = 28;
        if (r2 > r9) goto L_0x0107;
    L_0x00ee:
        r9 = b.b.h.u.E;
        if (r9 == 0) goto L_0x010d;
    L_0x00f2:
        r10 = r7.z;	 Catch:{ Exception -> 0x00ff }
        r11 = new java.lang.Object[r6];	 Catch:{ Exception -> 0x00ff }
        r12 = java.lang.Boolean.FALSE;	 Catch:{ Exception -> 0x00ff }
        r13 = 0;
        r11[r13] = r12;	 Catch:{ Exception -> 0x00ff }
        r9.invoke(r10, r11);	 Catch:{ Exception -> 0x00ff }
        goto L_0x010d;
    L_0x00ff:
        r9 = "MenuPopupWindow";
        r10 = "Could not invoke setTouchModal() on PopupWindow. Oh well.";
        android.util.Log.i(r9, r10);
        goto L_0x010d;
    L_0x0107:
        r9 = r7.z;
        r10 = 0;
        r9.setTouchModal(r10);
    L_0x010d:
        r9 = 23;
        if (r2 < r9) goto L_0x0117;
    L_0x0111:
        r9 = r7.z;
        r10 = 0;
        r9.setEnterTransition(r10);
    L_0x0117:
        r9 = r0.i;
        r10 = r9.size();
        r10 = r10 - r6;
        r9 = r9.get(r10);
        r9 = (b.b.g.i.d.d) r9;
        r9 = r9.a;
        r9 = r9.c;
        r10 = 2;
        r11 = new int[r10];
        r9.getLocationOnScreen(r11);
        r10 = new android.graphics.Rect;
        r10.<init>();
        r12 = r0.p;
        r12.getWindowVisibleDisplayFrame(r10);
        r12 = r0.q;
        if (r12 != r6) goto L_0x014a;
    L_0x013c:
        r12 = 0;
        r11 = r11[r12];
        r9 = r9.getWidth();
        r9 = r9 + r11;
        r9 = r9 + r5;
        r10 = r10.right;
        if (r9 <= r10) goto L_0x0150;
    L_0x0149:
        goto L_0x0152;
    L_0x014a:
        r12 = 0;
        r9 = r11[r12];
        r9 = r9 - r5;
        if (r9 >= 0) goto L_0x0152;
    L_0x0150:
        r9 = 1;
        goto L_0x0153;
    L_0x0152:
        r9 = 0;
    L_0x0153:
        if (r9 != r6) goto L_0x0157;
    L_0x0155:
        r13 = 1;
        goto L_0x0158;
    L_0x0157:
        r13 = 0;
    L_0x0158:
        r0.q = r9;
        r9 = 26;
        r10 = 5;
        if (r2 < r9) goto L_0x0164;
    L_0x015f:
        r7.p = r8;
        r2 = 0;
        r11 = 0;
        goto L_0x0199;
    L_0x0164:
        r2 = 2;
        r9 = new int[r2];
        r11 = r0.o;
        r11.getLocationOnScreen(r9);
        r2 = new int[r2];
        r8.getLocationOnScreen(r2);
        r11 = r0.n;
        r11 = r11 & 7;
        if (r11 != r10) goto L_0x018d;
    L_0x0177:
        r11 = 0;
        r12 = r9[r11];
        r14 = r0.o;
        r14 = r14.getWidth();
        r14 = r14 + r12;
        r9[r11] = r14;
        r12 = r2[r11];
        r14 = r8.getWidth();
        r14 = r14 + r12;
        r2[r11] = r14;
        goto L_0x018e;
    L_0x018d:
        r11 = 0;
    L_0x018e:
        r12 = r2[r11];
        r14 = r9[r11];
        r11 = r12 - r14;
        r2 = r2[r6];
        r9 = r9[r6];
        r2 = r2 - r9;
    L_0x0199:
        r9 = r0.n;
        r9 = r9 & r10;
        if (r9 != r10) goto L_0x01a6;
    L_0x019e:
        if (r13 == 0) goto L_0x01a1;
    L_0x01a0:
        goto L_0x01ac;
    L_0x01a1:
        r5 = r8.getWidth();
        goto L_0x01ae;
    L_0x01a6:
        if (r13 == 0) goto L_0x01ae;
    L_0x01a8:
        r5 = r8.getWidth();
    L_0x01ac:
        r11 = r11 + r5;
        goto L_0x01af;
    L_0x01ae:
        r11 = r11 - r5;
    L_0x01af:
        r7.f = r11;
        r7.k = r6;
        r7.j = r6;
        r7.j(r2);
        goto L_0x01d7;
    L_0x01b9:
        r2 = r0.r;
        if (r2 == 0) goto L_0x01c1;
    L_0x01bd:
        r2 = r0.t;
        r7.f = r2;
    L_0x01c1:
        r2 = r0.s;
        if (r2 == 0) goto L_0x01ca;
    L_0x01c5:
        r2 = r0.u;
        r7.j(r2);
    L_0x01ca:
        r2 = r0.a;
        if (r2 == 0) goto L_0x01d4;
    L_0x01ce:
        r10 = new android.graphics.Rect;
        r10.<init>(r2);
        goto L_0x01d5;
    L_0x01d4:
        r10 = 0;
    L_0x01d5:
        r7.x = r10;
    L_0x01d7:
        r2 = new b.b.g.i.d$d;
        r5 = r0.q;
        r2.m311init(r7, r1, r5);
        r5 = r0.i;
        r5.add(r2);
        r7.d();
        r2 = r7.c;
        r2.setOnKeyListener(r0);
        if (r4 != 0) goto L_0x0217;
    L_0x01ed:
        r4 = r0.w;
        if (r4 == 0) goto L_0x0217;
    L_0x01f1:
        r4 = r1.m;
        if (r4 == 0) goto L_0x0217;
    L_0x01f5:
        r4 = 2131492882; // 0x7f0c0012 float:1.8609228E38 double:1.0530974074E-314;
        r5 = 0;
        r3 = r3.inflate(r4, r2, r5);
        r3 = (android.widget.FrameLayout) r3;
        r4 = 16908310; // 0x1020016 float:2.387729E-38 double:8.353815E-317;
        r4 = r3.findViewById(r4);
        r4 = (android.widget.TextView) r4;
        r3.setEnabled(r5);
        r1 = r1.m;
        r4.setText(r1);
        r1 = 0;
        r2.addHeaderView(r3, r1, r5);
        r7.d();
    L_0x0217:
        return;
        */
        throw new UnsupportedOperationException("Method not decompiled: b.b.g.i.d.w(b.b.g.i.g):void");
    }
}
